from etgui import *
from PageLayout import *
from PrefsMirrorEntries import *
from AllModelDictEditors import *
from PrefsMirrorEntries import *

def MakePlatformSettingItems(model):
	return MakeItemIncludeDirectives(model,"IncludeMacro",) \
		+MakeSplitIncludeListEditor(model,"AddIncludeMacro","Additional","-I") \
		+MakeSplitStringListEditor(model,"PreprocessorSwitch","Preprocessor Directive(s)","-D")
	
def PlatformSettingPopup(model):
	path="PlatformSetting"
	submodel=PotGuiSubModel(path,model)
	# disable the submodel initially
	submodel.SetValue("PSEUDO.Prefix","")
	popup=PotGuiPopupButton("PSEUDO.Prefix")
	popup.AddItem("DEFAULT","<<DEFAULT>>")
	def UpdatePlatformSettingPopup(model,path=path,popup=popup):
		popup.RemoveAllItems()
		popup.AddItem("DEFAULT","<<DEFAULT>>")
		itemsList=Cast(model).GetIndex(path)
		print `path`,":",itemsList
		if path:
			path=path+'.'
		if itemsList:
			itemsList.sort()
			for v in itemsList:
				popup.AddItem(v,path+v)   # (display, model)
	model.AddModelObserver(UpdatePlatformSettingPopup,path)
	return MakeSectionLayout(
		ModelContainer(submodel,
			Form(
				items=["Platform",popup]+MakePlatformSettingItems(submodel)
			)
		)
	)

class PlatformSpecificEditor(ModelDictEditor):
	__super=ModelDictEditor
	def NewValue(self):
		return {
			"__frame__":"PlatformSetting",
			"Name":""
		}
	DoEdit=None
	DoCopy=None
	DoRename=None
	def GetIndex(self):
		return ["<<DEFAULT>>"]+self.__super.GetIndex(self)
	def DoMakePreview(self,model):
		return MakeSectionLayout(
			Form(
				items=MakePlatformSettingItems(model)
			),
		)
	def DoInsert(self,i,v):
		self.__super.DoInsert(self,i,v)
		self.Reload()
		return None

	def CanNew(self,i):
		return self.model.IsValueChangable(self.path) and len(self.GetNewNames())>0
	def CanDelete(self,i):
		return i>0 and self.model.IsValueChangable(self.path)
	def DoMakeItem(self,name):
		if name=="<<DEFAULT>>":
			name="Default Setting (Used when no platform specific Directives available)"
		return TextItem(name)

	def GetNewNames(self):
		nameList=[]
		items=self.model.GetIndex(self.path)
		if items is None:
			items=[]
		for i in EnvGetIndices("Platform"):
			if i not in items:
				nameList.append(i)
		nameList.sort()
		return nameList
	def EditName(self,how,key_value):
		key,value=key_value
		m=PotGuiModel()
		m.SetValue("Name",key)
		itemsList=self.GetNewNames()
		if not itemsList:
			ShowAlert(eAlertNote,
				"No Platfrorm specific information available!\n"
				"You have to set @BParser Configuration@B in the preferences."
			)
			return None
		popup=PotGuiPopupButton("Name")
		popup.RemoveAllItems()
		for v in itemsList:
			popup.AddItem(v,v)   # (display, model)
		if not key in itemsList:
			m.SetValue("Name",itemsList[0])
		retval=ShowSubDialog("%s %s" %(how,self.what),
			m,
			popup
		)
		newkey=m.GetValue("Name")
		value=self.RenameValue(value,newkey)
		if retval:
			return (newkey,value)
		else:
			return None
	def DoDelete(self,i):
		self.model.RemoveAt(self.PathAt(i))
		self.Reload()
		self.Select(self.GetSelected())
		return 0 # do not modify the list!
	def Observe1(self,*args):
		self.UpdateButtons()
	# Methods mor Merge support
	def DoSelect(self,i):
		self.__super.DoSelect(self,i)
		key=self.KeyAt(i)
		self.model.SetValue("PSEUDO.PlatformSetting_Merge.Select",key)
	def DoSetItems(self,model):
		if self.model is None:
			model.AddModelObserver(self.Observe1,"PSEUDO.Goto",self._GetCollView())
		return self.__super.DoSetItems(self,model)
	def ObserveMerge(self,model,path):
		if path=="PSEUDO.SetForAll.Done":
			self.ObserveReload(model,path)

	def MakeMergeOptions(self):
		if PotGuiVObj_InAddMarkButtons():
			self.model.AddModelObserver(self.ObserveMerge)
			return HBox(
				align=eVObjVBase,
				items=[
					"Merge Options:", 
					PotGuiVObj("PSEUDO.PlatformSetting_Merge.Replace",TextItem("Replace")),
					PotGuiVObj("PSEUDO.PlatformSetting_Merge.Add",TextItem("Add")),
					PotGuiVObj("PSEUDO.PlatformSetting_Merge.Delete",TextItem("Delete")),
				]
			)
		else:
			return None	


def MakePagePlatformSpecific(model):
	if EnvGetBool("GUI.Tools.Prefs.CrispAsPopup",0):
		settings=PlatformSettingPopup(model)
	else:
		editor=PlatformSpecificEditor(model,"PlatformSettings","Platform Setting")
		merge_options=editor.MakeMergeOptions()
		settings=MakeModelDictEditorLayout(editor,merge_options=merge_options)
	
	return MakePageLayout(eVObjHExpand|eVObjVExpand,
		settings
	)
	

